<?
class sym_gallery_admin extends symbiont{
    public function ___main($design){
        global $kernel, $sym, $sql;
        if($kernel->link->param2){
            $design=$this->_check($design, 'admin', 'gallery');
            $d=new design($design, 'gallery_admin', false);
            $this->gallerys='';
            $category=data::word($kernel->link->param2);
            $this->category_id=$sql->select_each('sym_category', 'id', array('name'=>$category), '', '', 1);
            
            $gallerys=$sql->query_each("SELECT v.id, v.name, p.title  FROM `sym_gallery` as v LEFT JOIN `paragraphs` as p ON v.paragraph_id=p.id LEFT JOIN `sym_category` as c ON c.id=v.category_id WHERE c.name='".$category."' AND (p.lng_id = ".$kernel->lang->id." OR p.title IS NULL) ORDER BY v.position");
            if(is_array($gallerys)){
                foreach($gallerys as $val){
                    $this->id=$val['id'];
                    $this->name=$val['name'];
                    $this->title=$val['title']?$val['title']:$val['name'];
                    $this->gallerys.=design::run($this->gallery);
                }
            }
            $d->inc();
        }
        else{
            $kernel->symbiont_add('category');
            $sym->category->___admin($design, 'gallery');
        }
    }
    public function ___add($design, $category=''){
        global $kernel, $sym, $sql;
        $kernel->symbiont_add('category-admin');
        $design=$this->_check($design, 'add', 'gallery');
        $d=new design($design, 'gallery', false);
        $sym->gallery->what='add';
        $sym->gallery->title='';
        $sym->gallery->content='';
        $sym->gallery->gallery_name='';
        $sym->gallery->categories='';
        $sym->gallery->parent_id=$category;
        
        $sym->gallery->infos='';
        $sym->gallery->languages='';
        $languages=$sql->select_each('languages', 'abbr', array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $sym->gallery->abbr=$val['abbr'];
            $sym->gallery->languages.=design::run($sym->gallery->language);
            $sym->gallery->infos.=design::run($sym->gallery->info);
        }
        
        $sym->gallery->categories=$sym->category_admin->pub_categories(0, 'gallery');
        $d->inc();
    }
    public function ___edit($design='', $id){
        global $sql, $kernel, $sym;
        $kernel->symbiont_add('category-admin');
        $design=$this->_check($design, 'add', 'gallery');
        $d=new design($design, 'gallery', false);
        
        $id=data::number($id);
        
        $cats=$sql->query_each("SELECT c.id, c.image, c.category_id, c.name, p.title, p.content, l.abbr  FROM `sym_gallery` as c LEFT JOIN `paragraphs` as p ON c.paragraph_id=p.id LEFT JOIN `languages` as l ON p.lng_id=l.id WHERE c.id=".$id."");
        
        $title=array();
        $content=array();
        if(is_array($cats)){
            foreach($cats as $val){
                $title[$val['abbr']]=$val['title'];
                $content[$val['abbr']]=$val['content'];
            }
            $sym->gallery->gallery_name=$cats[0]['name'];
            $sym->gallery->image=$cats[0]['image'];
            $sym->gallery->parent_id=$cats[0]['category_id'];
            $sym->gallery->current_id=$cats[0]['id'];
        }
        else{
            $sym->gallery->gallery_name='';
            $sym->gallery->image='';
            $sym->gallery->category_id='';
            $sym->gallery->current_id=0;
        }
        
        $sym->gallery->what='edit';
        $sym->gallery->infos='';
        $sym->gallery->languages='';
        $languages=$sql->select_each('languages', 'abbr', array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $sym->gallery->title=isset($title[$val['abbr']])?$title[$val['abbr']]:'';
            $sym->gallery->content=isset($content[$val['abbr']])?$content[$val['abbr']]:'';
            $sym->gallery->abbr=$val['abbr'];
            $sym->gallery->languages.=design::run($sym->gallery->language);
            $sym->gallery->infos.=design::run($sym->gallery->info);
        }
        $sym->gallery->categories=$sym->category_admin->pub_categories(0, 'gallery');
        $d->inc();
    }
    
    public function ___delete($design=''){
        $design=$this->_check($design, 'delete', 'gallery');
        $d=new design($design, 'category');
    }
    
    //DB functions
    public function ___db_add($design=''){
        global $sql, $user, $kernel;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])||!isset($_POST['title'])||!isset($_POST['title'])||!isset($_POST['image'])||!isset($_POST['category_id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $attention='';
        $kernel->library_add('paragraph');
        $name=data::word($_POST['name']);
        $category_id=data::number($_POST['category_id']);
        $p=new paragraph();
        foreach($_POST['title'] as $key=>$val){
            $title=data::html_remove($val);
            if(!$title) continue;
            $description=data::safe($_POST['description'][$key]);
            $abbr=data::word($key);
            $lng_id=$sql->select_each('languages', 'id', array('abbr'=>$abbr), '', '', 1);
            $p->add($lng_id, $title, $description);
        }
        $image=data::html_remove($_POST['image']);
        $cover='';
        if($image){
            $kernel->library_add('image');
            $img=new image($image);
            $cover=$img->resize_max('_m', 100);
            if(!$cover){
                $attention.="Can not resize file.";
            }
        }
        $id=$sql->insert('sym_gallery', array('name'=>$name, 'paragraph_id'=>$p->id, 'cover'=>$cover, 'image'=>$image, 'category_id'=>$category_id));
        $sql->update('sym_gallery', array('position'=>$id), array('id'=>$id), 1);
        if($id){
            print('{"ok": "Image added.", "attention": "'.$attention.'"}');
        }
        else{
            print('{"error": "Image adding error."}');
        }
    }
    
    public function ___db_edit($design=''){
        global $sql, $user, $kernel;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['name'])||!isset($_POST['title'])||!isset($_POST['category_id'])||!isset($_POST['title'])||!isset($_POST['image'])||!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $attention='';
        $kernel->library_add('paragraph');
        $id=data::number($_POST['id']);
        $old=$sql->query_each("SELECT c.cover, c.image, p.id FROM `sym_gallery` as c LEFT JOIN `paragraphs` as p ON c.paragraph_id=p.id WHERE c.id=".$id." LIMIT 1", true);
        $name=data::word($_POST['name']);
        if(isset($old['id'])&&$old['id']) $p=new paragraph($old['id']);
        else $p=new paragraph();
        foreach($_POST['title'] as $key=>$val){
            $title=data::html_remove($val);
            if(!$title) continue;
            $description=data::safe($_POST['description'][$key]);
            $abbr=data::word($key);
            $lng_id=$sql->select_each('languages', 'id', array('abbr'=>$abbr), '', '', 1);
            $p->set($lng_id, $title, $description);
        }
        $image=data::html_remove($_POST['image']);
        $cover='';
        $category_id=data::html_remove($_POST['category_id']);
        $values=array('name'=>$name, 'paragraph_id'=>$p->id, 'category_id'=>$category_id);
        if($image){
            if(isset($old['image'])&&$old['image']){
                unlink($old['image']);
            }
            if(isset($old['cover'])&&$old['cover']){
                unlink($old['cover']);
            }
            $kernel->library_add('image');
            $img=new image($image);
            $cover=$img->resize_max('_m', 100);
            if(!$cover){
                $attention.="Can not resize file.";
            }
            $values['image']=$image;
            $values['cover']=$cover;
        }
        $id=$sql->update('sym_gallery', $values, array('id'=>$id));
        if($id){
            print('{"ok": "Image edited.", "attention": "'.$attention.'"}');
        }
        else{
            print('{"error": "Image editing error."}');
        }
    }
    
    public function ___db_delete($design=''){
        global $user, $sql;
        if($user->access_level<9||!isset($_POST['id'])) return;
        $id=data::number($_POST['id']);
        $gallery=$sql->select_each('sym_gallery', array('cover', 'image'), array('id'=>$id), '', '', 1);
        if(file_exists($gallery['cover'])){
            unlink($gallery['cover']);
        }
        if(file_exists($gallery['image'])){
            unlink($gallery['image']);
        }
        $sql->delete('sym_gallery', array('id'=>$id));
    }
    public function ___db_sort($design=''){
        global $user, $sql;
        if($user->access_level<9) return;
        foreach($_POST['sort'] as $key=>$val){
            $id=data::number($val);
            $position=data::number($key)+1;
            $sql->update('sym_gallery', array('position'=>$position), array('id'=>$id), 1);
        }
    }
}
?>